Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

Web Service Endpoints

A Web service client can access J2EE applications in two ways. First, the client can access a Web service created with JAX-RPC. Behind the scenes, JAX-RPC uses a servlet to implement the Web service. (For more information on JAX-RPC, see the chapter, Building Web Services With JAX-RPC.) Second, a Web service client can access a stateless session bean through the service endpoint interface of the bean. Other types of enterprise beans cannot be accessed by Web service clients.

Provided that it uses the correct protocols (SOAP, HTTP, WSDL), any Web service client can access a stateless session bean, whether or not the client is written in the Java programming language. The client doesn't even "know" what technology implements the service--stateless session bean, JAX-RPC, or some other technology. Because of this flexibility, you can integrate J2EE applications with Web services.

Web Service Example: HelloServiceEJB

The files for this example are in the <INSTALL>/j2eetutorial14/examples/ejb/helloservice/ directory.

HelloServiceEJB is a stateless session bean that implements a single method, sayHello. This method matches the sayHello invoked by the clients described in Creating Web Service Clients with JAX-RPC. Later in this section, you'll test the HelloServiceEJB by running one of these JAX-RPC clients.

Source Code for HelloServiceEJB

The source files for this example, HelloService.java and HelloServiceBean.java, are located in the helloservice/src/ subdirectory.

Web Service Endpoint Interface

HelloIF is the bean's Web service endpoint interface. It provides the client's view of the Web service, hiding the stateless session bean from the client. A Web service endpoint interface must conform to the rules of a JAX-RPC service endpoint interface. For a summary of those rules, see Coding the Service Endpoint Interface and Implementation Class. Here is the source code for the HelloIF interface:

package helloservice;
import java.rmi.RemoteException;
import java.rmi.Remote;

public interface HelloIF extends Remote {
 
   public String sayHello(String name) throws RemoteException;
} 

Stateless Session Bean Implementation Class

The HelloServiceBean class implements the sayHello method defined by the HelloService interface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces to HelloServiceEJB, the methods of the HelloServiceBean class could also be accessed by remote clients that are J2EE components. No changes to the HelloServiceBean class would be necessary. The source code for the HelloServiceBean class follows:

package helloservice;
import java.rmi.RemoteException; 
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HelloServiceBean implements SessionBean {

   public String sayHello(String name) {

      return "Hello " + name + "from HelloServiceEJB";
   }

   public HelloServiceBean() {}
   public void ejbCreate() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sc) {}
}  

Building HelloServiceEJB

In a terminal window, go to the <INSTALL>/j2eetutorial14/examples/ejb/helloservice/ directory. To build HelloServiceEJB, type the following command:

asant build-service 

This command performs the following tasks:

The wscompile tool writes the MyHelloService.wsdl file to the helloservice/build/ subdirectory. For more information about the wscompile tool, see Building Web Services With JAX-RPC.

Use deploytool to package and deploy this example.

Creating the Application

In this section, you'll create a J2EE application named HelloService, storing it in the file HelloService.ear.

  1. In deploytool, select FileRight ArrowNewRight ArrowApplication EAR.
  2. Click Browse.
  3. In the file chooser, navigate to <INSTALL>/j2eetutorial14/examples/ejb/helloservice/.
  4. In the File Name field, enter HelloService.ear.
  5. Click New Application.
  6. Click OK.
  7. Verify that the HelloService.ear file resides in <INSTALL>/j2eetutorial14/examples/ejb/helloservice/.

Packaging the Enterprise Bean

Start the Edit Enterprise Bean wizard by selecting FileRight ArrowNewRight ArrowEnterprise JavaBean JAR. The wizard displays the following dialog boxes.

  1. Introduction dialog box
    1. Read the explanatory text for an overview of the wizard's features.
    2. Click Next.
  2. EJB JAR dialog box
    1. Select the button labelled Create New JAR Module in Application.
    2. In the combo box below this button, select HelloService.
    3. In the JAR Display Name field, enter HelloServiceJAR.
    4. Click Edit.
    5. In the tree under Available Files, locate the <INSTALL>/j2eetutorial14/examples/ejb/helloservice/build/ directory.
    6. In the Available Files tree select mapping.xml and MyHelloService.wsdl.
    7. Select these classes from the .../helloservice/build/helloservice/ directory: HelloIF.class and HelloServiceBean.class.
    8. Click Add.
    9. Click OK.
    10. Click Next.
  3. General dialog box
    1. Under Bean Type, select the Session button.
    2. Select the Stateless button.
    3. In the Enterprise Bean Class combo box, select helloservice.HelloServiceBean.
    4. In the Enterprise Bean Name field, enter HelloServiceEJB.
    5. Click Next.
  4. In the Configuration Options dialog box, click Next. The wizard will automatically select the Yes button for Expose Bean as Web Service Endpoint.
  5. In the Choose Service dialog box:
    1. Select META-INF/wsdl/MyHelloService.wsdl in the WSDL File combo box.
    2. Select mapping.xml from the Mapping File combo box.
    3. Make sure MyHelloService is in the Service Name and Service Display Name edit boxes.
  6. In the Web Service Endpoint dialog box:
    1. Select helloservice.HelloIF in the Service Endpoint Interface combo box.
    2. In the WSDL Port section, set the Namespace to urn:Foo, and the Local Part to HelloIFPort.
    3. In the Deployment Settings section, set the Endpoing Address URI to hello-ejb/hello.
    4. Click Next.
  7. In the Review Settings dialog box:
    1. Optional: Examine the EJB deployment descriptor.
    2. Click Finish.

Deploying the Enterprise Application

Now that the J2EE application contains the enterprise bean, it is ready for deployment.

  1. Select the HelloService application.
  2. Select ToolsRight ArrowDeploy.
  3. Under Connection Settings, enter the user name and password for the J2EE application server.
  4. Click OK.
  5. In the Distribute Module dialog box click Close when the deployment completes.
  6. Verify the deployment.
    1. In the tree, expand the Servers node and select the host that is running the J2EE application server.
    2. In the Deployed Objects table, make sure that HelloService is listed and its status is Running.

Building the Web Service Client

In the next section, to test the Web service implemented by HelloServiceEJB, you will run the JAX-RPC client described in Building Web Services With JAX-RPC.

To build the static stub client, perform these steps:

  1. Go to the <INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/ directory and type:
  2.   asant build 
    
  3. Go to the <INSTALL>/j2eetutorial14/examples/jaxrpc/staticstub/ directory and type:
  4.   asant build 
    
  5. Edit the build.properties file and change the endpoint.address property to
  6.   http://localhost:1024/hello-ejb/hello 
    

For details about creating the JAX-RPC service and client, see these sections: Creating a Web Service with JAX-RPC and Static Stub Client Example.

Running the Web Service Client

Verify that the HelloServiceEJB has been deployed by clicking on the target application server in the Servers tree in deploytool. In the Deployed Objects tree you should see HelloService.

To run the client, go to the <INSTALL>/j2eetutorial14/examples/jaxrpc/staticstub/ directory and enter:

asant run 

The client should display the following line:

Hello Duke! (from HelloServiceEJB) 
Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.